package org.duiduo.controller;

import lombok.extern.slf4j.Slf4j;
import org.duiduo.config.annotation.LogAop;
import org.duiduo.entity.DemoEventEntity;
import org.duiduo.pojo.Address;
import org.duiduo.protocol.DemoClient;
import org.duiduo.service.AddressService;
import org.duiduo.util.ThreadMDCUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.core.handle.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 控制层 这个demo不放行 需要先登录
 * 先登录 然后请求头放 Authorization 和 token值
 *
 * @author liangze
 * @date 2025/9/27 15:17
 */
@Slf4j
@Controller
public class DemoController implements DemoClient {

    // 取配置 autoRefreshed 自动刷新配置
    @Inject(value = "${test}", autoRefreshed = true)
    private String test;

    @Inject
    private AddressService addressService;

    @Mapping("/hello")
    public String hello(Context ctx) {
        return "Hello " + ctx.paramOrDefault("name", "world");
    }

    @LogAop(title = "查询所有地址")
    @Mapping("/getAll")
    @Override
    public List<Address> getAll() {
        log.info("nacosTest:{}", test);
        return addressService.selectAll();
    }

    @Mapping("/postAll")
    @Post
    public List<Address> postAll(Address address) {
        log.info("code:{}", address.getCode());
        return addressService.selectAll();
    }

    @LogAop(title = "消息发送测试")
    @Mapping("/eventDemo")
    public void eventDemo() {
        Map<String, Object> msg = new HashMap<>();
        msg.put(ThreadMDCUtil.TRACE_ID, ThreadMDCUtil.getTraceId());
        msg.put("name", "张三");
        // 1. 原始发送事件
        /*Event event = new Event(RabbitMqConstant.DEMO_TOPIC, JSONUtil.toJsonStr(msg))
                .group(RabbitMqConstant.GROUP);
        CloudClient.event().publish(event);*/

        // 2. 实体类发送事件
        DemoEventEntity entity = new DemoEventEntity();
        entity.setTraceId(ThreadMDCUtil.getTraceId());
        entity.setName("张三");
        entity.publish();
    }

}
